import { Component, OnInit, EventEmitter, Input, Output, ElementRef, ViewChild } from '@angular/core';

import { Service } from '../../../server/server';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Http } from '@angular/http';
import { HintsService } from '../../../public/serve/hints/hints.service';
declare var $: any;


@Component({
  selector: 'app-management-update-password',
  templateUrl: './management-update-password.component.html'
})
export class ManagementUpdatePasswordComponent implements OnInit {

  constructor(
    private server: Service, 
    private http: HttpClient,
    private hintsService :HintsService 
  ) { }

  ngOnInit() {
    //console.log(this.oldPassword)
  }

  //清空表单
  @ViewChild('myform') formElement: HTMLFormElement;
  reset() {
    this.formElement.reset();
    $('#password').modal('hide');
  }

  @Input() getTabVal: any;
  //输入旧密码
  updateData: oldpasswords = {
    oldPasswords: '',
    uselessPassword: '',
    newPassword: ''
  };
  //旧密码
  public oldPassword: any;
  //比较密码
  originalPassword() {
    let url = this.server.baseUrl+'admin/pusers/checkpw';
    let body = {
      userId: this.getTabVal.userId,
      password: this.updateData.oldPasswords
    }
    //console.log(body)
    this.http.post<Response>(url, body).subscribe((data: any) => {
      //console.log(data)
      if (data.result_code == '0000') {
        this.oldPassword = true;
      } else {
        this.oldPassword = false;
      }
    })
  }
  //修改密码
  updatepassword() {
    let url = this.server.baseUrl+'admin/pusers/modifypw';
    let body = {
      userId: this.getTabVal.userId,
      password: this.updateData.newPassword
    }
    //console.log(body)
    this.http.post<Response>(url, body).subscribe((res: any) => {
      //console.log(res)
      this.hintsService.messages(res.message,res.result_code);
      if (res.result_code === '0000') {
        $('#password').modal('hide');
        //prompt
        this.formElement.reset();
        //update
      }
    })
  }
}

export class oldpasswords {
  oldPasswords?: string;
  uselessPassword?: string;
  newPassword?: string;
}
